#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include<conio.h>
#include"FuncStruct.h"

int JudgeColorValue(Info conf)
{
	short flag_i[20][20]={0};
	short oneColor[100]={0};
	int oneColorValue=0;
	int colorValue=0;
	int i,j,k,l;
	int flag_j=0,color=1;
	for(color=0;color<conf.clourN;color++)
	{
		flag_j=1;
	for(i=0;i<conf.matrixN;i++)
			for(j=0;j<conf.matrixN;j++)
				flag_i[i][j]=0;	
	for(i=0;i<100;i++)
		oneColor[i]=0;
	oneColorValue=0;
	for(i=0;i<conf.matrixN;i++)
	{
		for(j=0;j<conf.matrixN;j++)
			if(conf.matrixArr[i][j]==color)	
		{	flag_i[i][j]=flag_j;
			flag_j++;
			break;
		}
		
		if(flag_i[i][j]==1)
			break;
	}


		for(i=0;i<conf.matrixN;i++)
		{
			for(j=0;j<conf.matrixN;j++)
			{		

				if(flag_i[i][j]==0&&conf.matrixArr[i][j]==color)
				{
					flag_i[i][j]=flag_j;
					flag_j++;
				}
				i=i;
				if(flag_i[i][j]!=0)
				{
				if(i!=conf.matrixN-1&&conf.matrixArr[i+1][j]==color&&flag_i[i+1][j]==0)
						flag_i[i+1][j]=flag_i[i][j];
				 if(flag_i[i][j]!=0&&flag_i[i][j+1]!=0&&j!=conf.matrixN-1&&flag_i[i][j]!=flag_i[i][j+1])
									
					for(k=0;k<conf.matrixN;k++)
						for(l=0;l<conf.matrixN;l++)
						if(flag_i[k][l]==flag_i[i][j])
								flag_i[k][l]=flag_i[i][j+1];
				if(flag_i[i][j]!=0&&flag_i[i+1][j]!=0&&i!=conf.matrixN-1&&flag_i[i][j]!=flag_i[i+1][j])
					for(k=0;k<conf.matrixN;k++)
						for(l=0;l<conf.matrixN;l++)
							if(flag_i[k][l]==flag_i[i][j])
								flag_i[k][l]=flag_i[i+1][j];
				if(j!=0&&conf.matrixArr[i][j-1]==color&&flag_i[i][j-1]==0)
						flag_i[i][j-1]=flag_i[i][j];
				if(j!=conf.matrixN-1&&conf.matrixArr[i][j+1]==color&&flag_i[i][j+1]==0)
						flag_i[i][j+1]=flag_i[i][j];
				if(flag_i[i][j]!=0&&flag_i[i-1][j]!=0&&i!=0&&flag_i[i][j]!=flag_i[i-1][j])
					for(k=0;k<conf.matrixN;k++)
						for(l=0;l<conf.matrixN;l++)
							if(flag_i[k][l]==flag_i[i][j])
								flag_i[k][l]=flag_i[i-1][j];	
				}
				



			}
				

		}
		/*
	for(k=0;k<conf.matrixN;k++)
		{	for(l=0;l<conf.matrixN;l++)
				printf("%d ",flag_i[k][l]);
				printf("\n");}
		printf("\n");
		*/

	/*	for(k=0;k<conf.matrixN;k++)
		{	for(l=0;l<conf.matrixN;l++)
				printf("%d ",flag_i[k][l]);
				printf("\n");}
		printf("\n");
*/
		for(i=0; i<conf.matrixN; i++)
			for(j=0;j<conf.matrixN;j++)
				if(flag_i[i][j]!=0)
					oneColor[flag_i[i][j]]++;

		for(i=1; i<100; i++)
			if(oneColor[i] != 0)
			{	oneColorValue=oneColorValue > oneColor[i] ? oneColorValue : oneColor[i];}

	colorValue+=oneColorValue;
	}
	
	return colorValue;
}